/* eslint-disable @typescript-eslint/no-explicit-any */
import { Module } from 'vuex'
import { clientSetting } from '@/config/client-setting'

export interface TabsItem {
  name: string; // 路由名
  path: string; // 路径
  title: string; // 路由中文名
  isDefault?: boolean // 默认tab
  query?: {
    [key: string]: any;
  },
  params?: {
    [key: string]: any;
  }
}
export interface TabsState {
  layoutTabs: TabsItem[]
}

const defaultTab = {
  name: '',
  path: clientSetting.redirect.path,
  title: clientSetting.redirect.title,
  isDefault: true
}

const tabs: Module<TabsState, TabsItem> = {
  namespaced: true,
  state: () => ({
    layoutTabs: [defaultTab]
  }),
  getters: {
    getLayoutTabs (state) {
      return state.layoutTabs
    }
  },
  mutations: {
    SET_LAYOUT_TABS (state, payload) {
      state.layoutTabs = payload
    }
  },
  actions: {
    addTab ({ commit, state }, data) {
      commit('SET_LAYOUT_TABS', [...state.layoutTabs, data])
    },
    removeTab ({ commit, state }, data) {
      const value = state.layoutTabs.filter(v => v.path !== data.path)
      commit('SET_LAYOUT_TABS', value)
    },
    clearAllTabs ({ commit }) {
      commit('SET_LAYOUT_TABS', [defaultTab])
    },
    clearOtherTabs ({ commit }, data) {
      const value = data.path === defaultTab.path ? [defaultTab] : [defaultTab, data]
      commit('SET_LAYOUT_TABS', value)
    },
    closeLeft ({ commit, state }, data) {
      const _index = state.layoutTabs.findIndex(v => v.path === data.path)
      const right = state.layoutTabs.slice(_index)
      const value = data.path === defaultTab.path ? right : [defaultTab, ...right]
      commit('SET_LAYOUT_TABS', value)
    },
    closeRight ({ commit, state }, data) {
      const _index = state.layoutTabs.findIndex(v => v.path === data.path)
      const value = state.layoutTabs.slice(0, _index + 1)
      commit('SET_LAYOUT_TABS', value)
    }
  }
}

export default tabs
